package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.CpcActivityMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CpcActivityEnrollMapper;
import com.ruoyi.community.service.ICpcActivityEnrollService;

/**
 * 党建活动报名Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@Service
public class CpcActivityEnrollServiceImpl implements ICpcActivityEnrollService 
{
    @Autowired
    private CpcActivityEnrollMapper cpcActivityEnrollMapper;

    @Autowired
    private CpcActivityMapper cpcActivityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询党建活动报名
     * 
     * @param id 党建活动报名ID
     * @return 党建活动报名
     */
    @Override
    public CpcActivityEnroll selectCpcActivityEnrollById(String id)
    {
        return cpcActivityEnrollMapper.selectCpcActivityEnrollById(id);
    }

    /**
     * 查询党建活动报名列表
     * 
     * @param cpcActivityEnroll 党建活动报名
     * @return 党建活动报名
     */
    @Override
    public List<CpcActivityEnroll> selectCpcActivityEnrollList(CpcActivityEnroll cpcActivityEnroll)
    {
        return cpcActivityEnrollMapper.selectCpcActivityEnrollList(cpcActivityEnroll);
    }

    @Override
    public List<SysUser> selectCpcActivityEnrollListById(String id)
    {
        List<CpcActivityEnroll> listEnroll= cpcActivityEnrollMapper.selectCpcActivityEnrollListById(id);
        List<SysUser> listUser = new ArrayList<>();
        SysUser query = new SysUser();
        for (CpcActivityEnroll cpcActivityEnroll : listEnroll) {
//            SysUser enrollUser = sysUserMapper.selectUserById(1L, 1);
            query.setUserId(cpcActivityEnroll.getUserId());
            query.setCommunityId(SecurityUtils.getCommunityId());
            List<SysUser> enrollUsers= sysUserMapper.selectUserOnlyOnUser(query);
            listUser.addAll(enrollUsers);
        }
        return listUser;
    }

    /**
     * 新增党建活动报名
     * 
     * @param cpcActivityEnroll 党建活动报名
     * @return 结果
     */
    @Override
    public int insertCpcActivityEnroll(CpcActivityEnroll cpcActivityEnroll)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        cpcActivityEnroll.setUserId(loginUser.getUserId());
        int hasEnrolled = cpcActivityEnrollMapper.hasEnrolled(cpcActivityEnroll.getUserId().toString(), cpcActivityEnroll.getActivityId());
        if(hasEnrolled==1){
            return -1;
        }else{
            CpcActivity cpcActivity = cpcActivityMapper.selectCpcActivityById(cpcActivityEnroll.getActivityId());
            Long enrollNumber = cpcActivity.getEnrollNumber() + 1;
            cpcActivity.setEnrollNumber(enrollNumber);
            cpcActivityMapper.updateCpcActivity(cpcActivity);
            return cpcActivityEnrollMapper.insertCpcActivityEnroll(cpcActivityEnroll);
        }
    }

    public boolean hasEnrolled(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasEnrolled = cpcActivityEnrollMapper.hasEnrolled(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        return hasEnrolled==1;
    }

    /**
     * 修改党建活动报名
     * 
     * @param cpcActivityEnroll 党建活动报名
     * @return 结果
     */
    @Override
    public int updateCpcActivityEnroll(CpcActivityEnroll cpcActivityEnroll)
    {
        return cpcActivityEnrollMapper.updateCpcActivityEnroll(cpcActivityEnroll);
    }

    /**
     * 批量删除党建活动报名
     * 
     * @param ids 需要删除的党建活动报名ID
     * @return 结果
     */
    @Override
    public int deleteCpcActivityEnrollByIds(String[] ids)
    {
        return cpcActivityEnrollMapper.deleteCpcActivityEnrollByIds(ids);
    }

    /**
     * 删除党建活动报名信息
     * 
     * @param id 党建活动报名ID
     * @return 结果
     */
    @Override
    public int deleteCpcActivityEnrollById(String id)
    {
        return cpcActivityEnrollMapper.deleteCpcActivityEnrollById(id);
    }
}
